//
//  HomeWebsiteFlipView.m
//  BaoVn
//
//  Created by Le Anh Tai on 11/8/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "HomeWebsiteFlipView.h"
#import "HomeWebsiteFlipItemView.h"
#import "WebsiteViewController.h"

@implementation HomeWebsiteFlipView

- (instancetype)init
{
    if (self = [super init])
    {
        _data = [[NSMutableArray alloc] init];
        _newsType = [AppInfo shared].newsType;
        
        //dummy data
        for (NSInteger i = 0; i < 100; i++)
        {
            NSString *urlImage;
            if (i % 4 == 3)
            {
                urlImage = @"https://lh6.googleusercontent.com/-K9pfh7uMfvk/AAAAAAAAAAI/AAAAAAAAABU/WxnLwQn8mqE/photo.jpg";
            }
            else if (i % 4 == 2)
            {
                urlImage = @"http://files.gamebanana.com/img/ico/sprays/50425de3b92fe.jpg";
            }
            else if (i % 4 == 1)
            {
                urlImage = @"https://lh4.googleusercontent.com/-FofXcx30zwE/AAAAAAAAAAI/AAAAAAAABxI/2k3sBf3DbqU/photo.jpg";
            }
            else
            {
                urlImage = @"https://lh6.googleusercontent.com/-LLIm_TOEbnU/AAAAAAAAAAI/AAAAAAAAAGA/JK4rtIhSroM/photo.jpg";
            }
            
            [_data addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                              urlImage, @"image",
                              @"Yamaha launched its fresh 850cc street-focused triple. ", TITLE_KEY,
                              @"The new bike was especially created for the European market and features a sharp style, an upright riding position and a modern 850cc traverse three cylinder power plant.", DESCRIPTION_KEY,
                              [NSString stringWithFormat:@"%ld gio truoc", i] , CREATED_DATE_KEY, nil]];
        }
        
        [_detailCurrentView loadDataWithValue:[self dataForViewAtIndex:0]];
    }
    
    return self;
}

- (BaseFlipViewItem *)instanceForCurrentSwipeItem
{
    return [[HomeWebsiteFlipItemView alloc] init];
}

- (BaseFlipViewItem*)instanceForTempSwipeItem
{
    return [[HomeWebsiteFlipItemView alloc] init];
}

- (id)dataForViewAtIndex:(NSInteger)index
{
    NSMutableArray *data = [[NSMutableArray alloc] init];
    
    if (_newsType == NewsTypeItem)
    {
        NSInteger realIndex = index * [self numberOfItemsForEachFlip];
        for (NSInteger i = realIndex; i < realIndex + [self numberOfItemsForEachFlip]; i++)
        {
            [data addObject:[_data objectAtIndex:i]];
        }
    }
    else
    {
        [data addObject:[_data objectAtIndex:index]];
    }
    
    return data;
}

- (void)switchToNewsType:(NewsType)newsType
{
    if (_newsType == newsType)
    {
        return;
    }
    
    _newsType = newsType;
    
    [self calculateSelectedIndex];
    
    ((HomeWebsiteFlipItemView *)_detailCurrentView).newsType = _newsType;
    ((HomeWebsiteFlipItemView *)_detailTempView).newsType = _newsType;
    
    [_detailCurrentView loadDataWithValue:[self dataForViewAtIndex:self.selectedIndex]];
    _detailCurrentView.frame = _detailCurrentView.frame;
}

- (void)calculateSelectedIndex
{
    NSInteger selectedIndex = self.selectedIndex;
    
    //switch to type item
    if (_newsType == NewsTypeItem)
    {
        selectedIndex = selectedIndex/[self numberOfItemsForEachFlip];
    }
    //switch to type card
    else
    {
        selectedIndex = selectedIndex*[self numberOfItemsForEachFlip];
    }
    
    self.selectedIndex = selectedIndex;
}

@end
